package weaver.jobs.zhlx.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.zhlx.duss.utils.ZhlxJobUtil;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-01-15
 * @Description: 定时调用SAP-FI013接口同步SD银行代码到建模台账
 * @ModifyLog:
 **/

public class WeaSynSDYhdmBySapCron extends BaseCronJob {

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        try {
            ZhlxJobUtil jobUtil = new ZhlxJobUtil();
            String tablename = "uf_sd_bk_jm";
            String currentDate = DateUtil.getCurrentDate();
            int formmodeid = jobUtil.getFormmodeid(tablename);
            JSONObject dataJson = new JSONObject();
            JSONObject headerJson = new JSONObject();
            headerJson.put("ERDAT", currentDate.replace("-", ""));
            dataJson.put("HEADER", headerJson);
            JSONObject resultJson = jobUtil.handleOKHttpRequest("FI013", dataJson);
            String code = Util.null2String(resultJson.getString("code"));
            baseBean.writeLog(">>>>>WeaSynSDYhdmBySapCron>>>>>code=" + code);
            if ("success".equals(code)) {
                String output = Util.null2String(resultJson.getString("OUTPUT"));
                JSONObject outputJson = JSONObject.parseObject(output);
                String msgType = outputJson.getString("MSG_TYPE");
                if ("S".equalsIgnoreCase(msgType)) {
                    String dataReturnStr = outputJson.getString("DATA");
                    JSONObject dataReturnJson = JSONObject.parseObject(dataReturnStr);
                    String returnData = dataReturnJson.getString("RETURN");
                    JSONArray returnList = JSONArray.parseArray(returnData);
                    for (int i = 0; i < returnList.size(); i++) {
                        JSONObject paramJson = new JSONObject();
                        JSONObject returnJson = returnList.getJSONObject(i);
                        String yxdm = returnJson.getString("BANKL");
                        //银行所在国家/地区代码
                        paramJson.put("yxszgjdqdm", returnJson.getString("BANKS"));
                        //国家名称
                        paramJson.put("gjmc", returnJson.getString("LANDX50"));
                        //银行代码
                        paramJson.put("yxdm", yxdm);
                        //银行名称
                        paramJson.put("yxmc", returnJson.getString("BANKA"));
                        //地区
                        paramJson.put("dqszzqzxs", returnJson.getString("PROVZ"));
                        //地区名称
                        paramJson.put("dqmc", returnJson.getString("BEZEI"));
                        //街道和房屋号
                        paramJson.put("jdhfwh", returnJson.getString("STRAS"));
                        //城市
                        paramJson.put("cs", returnJson.getString("ORT01"));
                        //分行
                        paramJson.put("fx", returnJson.getString("BRNCH"));
                        //国际付款的
                        paramJson.put("gjfkdswiftbic", returnJson.getString("SWIFT"));
                        //删除标识
                        paramJson.put("scbs", returnJson.getString("LOEVM"));
                        //最新变更日期
                        paramJson.put("zxbgrq", returnJson.getString("ERDAT"));
                        int mainid = jobUtil.getMainId(tablename, "yxdm", yxdm);
                        if (mainid <= 0) {
                            jobUtil.insertModelData(formmodeid, paramJson);
                        } else {
                            jobUtil.updateModelData(tablename, mainid, paramJson);
                        }
                    }
                }
            }
        } catch (Exception e) {
            baseBean.writeLog(">>>>>WeaSynXsddtzBySapCron>>>>>Exception=" + e);
        }

    }

}

